package com.llf.usercenter.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisTemplateConfig {

    @Bean
    //@ConditionalOnSingleCandidate 注解用来判断指定类在 BeanFactory 中是否只有一个实例：
    //（1）如果在 BeanFactory 中存在多个实例，则匹配失败；
    //（2）如果在 BeanFactory 中仅仅存在一个实例，则匹配成功；
    @ConditionalOnSingleCandidate
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(RedisSerializer.string());
        return redisTemplate;
    }
}
